// console.log(jQuery);

/*

https://learn.jquery.com/ajax/key-concepts/



https://api.jquery.com/jQuery.ajax/



案例
https://learn.jquery.com/ajax/jquery-ajax-methods/




personal_token= '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'

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

*/

// 1.ajax 基本使用
// $.ajax('https://api.github.com/users/changyubiao',{
// 	method:'GET'

// }).done(function(data){

// 	console.log(data);

// });




var $search = $('#search-form');

var $button = $('#search-btn');

var $input = $('input#username');

var $searchResult = $('#search-result');


const gitToken = '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';


$search.on('submit', function(e) {

	e.preventDefault();

	console.log('click btn ...');

	let usernanme = $input.val().trim() || 'frank';

	$.ajax('https://api.github.com/users/' + usernanme, {
		method: 'GET',
		headers: {
			"Authorization": `token ${gitToken}`
		}
	}).done(function(data) {

		console.log(data);
		var searchHtml = `
		<div>
			name: ${data.name}	
		</div>
		
		<div>
			blog: ${data.blog}	
		</div>
		<div>
			bio: ${data.bio}	
		</div>`;

		$searchResult.html(searchHtml);

	}).fail(function(xhr, status, errorThrown) {
		// alert("Sorry, there was a problem!");
		console.error("Sorry, there was a problem!");
		console.log("Error: " + errorThrown);
		console.log("Status: " + status);
		console.dir(xhr);
	})


})




// $button.on('click', function(e) {

// 	e.preventDefault();

// 	console.log('click btn ...');

// 	let usernanme = $input.val().trim()  || 'frank';	

// 	$.ajax('https://api.github.com/users/' + usernanme , {
// 		method: 'GET',
// 		headers: {
// 			"Authorization": `token ${gitToken}`
// 		}
// 	}).done(function(data) {

// 		console.log(data);
// 		var searchHtml = `
// 		<div>
// 			name: ${data.name}	
// 		</div>

// 		<div>
// 			blog: ${data.blog}	
// 		</div>
// 		<div>
// 			bio: ${data.bio}	
// 		</div>`;

// 		$searchResult.html(searchHtml);

// 	}).fail(function(xhr, status, errorThrown) {
// 		alert("Sorry, there was a problem!");
// 		console.log("Error: " + errorThrown);
// 		console.log("Status: " + status);
// 		console.dir(xhr);
// 	})


// })
